package com.qunar.homework3.service;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.io.Files;
import com.google.common.io.Resources;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;


import javax.xml.bind.ValidationException;
import java.io.File;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RecoveryFiction {

    private static final Pattern RE_PATTERN = Pattern.compile("\\$(natureOrder|indexOrder|charOrder|charOrderDESC)\\(([0-9]{1,})\\)");

    public boolean Recovery(String sdxlPpropUrl, String sdxlTemplateUrl, String sdxlPath) {
        try {
            ImmutableList<String> propLines = Resources.asCharSource(new URL(sdxlPpropUrl), Charset.forName("utf-8")).readLines();
            ImmutableList<String> mapLines = Resources.asCharSource(new URL(sdxlTemplateUrl), Charset.forName("utf-8")).readLines();
            List<Map.Entry<Integer, String>> natureOrder = getMapListFromLines(mapLines);
            Map<Integer, String> indexOrder = new HashMap<>();
            for (Map.Entry<Integer, String> entry : natureOrder){
                indexOrder.put(entry.getKey(), entry.getValue());
            }
            List<Map.Entry<Integer, String>> charOrder = getSortResults(natureOrder, Comparator.comparing(Map.Entry::getValue));
            List<Map.Entry<Integer, String>> charOrderDESC = getSortResults(natureOrder, (e1, e2) -> e2.getValue().compareTo(e1.getValue()));
            Map<String, Function<Integer, String>> replacesMapper = new HashMap<>();
            replacesMapper.put("natureOrder", entry -> natureOrder.get(entry).getValue());
            Matcher matcher;
            StringBuilder sb = new StringBuilder();
            for (String propLine : propLines){
                matcher = RE_PATTERN.matcher(propLine);
                while (matcher.find()){
                    sb.append(replacesMapper.get(matcher.group(0)).apply(matcher.group(1)));
                }
            }
        }catch (Exception e){
            return false;
        }

        return true;
    }

    private List<Map.Entry<Integer, String>> getSortResults(List<Map.Entry<Integer, String>> list, Comparator<Map.Entry<Integer, String>> sortComparator) {
        if (CollectionUtils.isEmpty(list) || sortComparator == null){
            throw new RuntimeException("sorrt args error");
        }
        List<Map.Entry<Integer, String>> resList= new ArrayList<>(list);
        Collections.sort(resList, sortComparator);
        return resList;
    }

    private List<Map.Entry<Integer, String>> getMapListFromLines(ImmutableList<String> mapLines) {
        List<Map.Entry<Integer, String>> resList = null;
        if (CollectionUtils.isEmpty(mapLines)) {
            return resList;
        }
        if (!CollectionUtils.isEmpty(mapLines)) {
            Map<Integer, String> map = new HashMap<>();
            String[] pair;
            for (String l : mapLines) {
                if (StringUtils.isNotBlank(l)) {
                    try {
                        pair = StringUtils.split("\t");
                        map.put(Integer.parseInt(pair[0]), pair[1]);
                    } catch (Exception e) {
                        System.out.println("something wrong :");
                        e.printStackTrace();
                    }
                    pair = StringUtils.split("\t");
                    map.put(Integer.parseInt(pair[0]), pair[1]);
                }
            }
        }
        return resList;
    }
}
